package com.tian.view.draw.advance

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.BlurMaskFilter
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.EmbossMaskFilter
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.tian.util.view.DisplayUtil
import com.tian.view.R

/**
 * @author tiansongtao
 * @date 2023/1/4 16:16
 *
 * 发光效果
 * [Paint.setMaskFilter] 设置画笔发光效果
 *  参数：maskfilter：MaskFilter没有具体实现，子类的具体实现有
 *   [BlurMaskFilter]: 发光效果
 *      BlurMaskFilter(float radius, Blur style):构造函数
 *         redius:发光模糊效果半径
 *         style: 发光样式：
 *
 *   [EmbossMaskFilter]: 浮雕效果,已弃用
 *
 *
 */
class MaskFilterView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
        View(context, attrs, defStyleAttr) {

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null)

    private val mBitmap: Bitmap
    private val mAlphaBitmap: Bitmap


    init {
        setLayerType(LAYER_TYPE_SOFTWARE, null)
        mBitmap = BitmapFactory.decodeResource(resources, R.drawable.jingdong)!!
        mAlphaBitmap = mBitmap.extractAlpha()
    }

    val paint = Paint().apply {
        color = Color.RED
        strokeWidth = DisplayUtil.dpToPx(2f)
        style = Paint.Style.FILL_AND_STROKE
        isAntiAlias = true
    }
    val textPaint = Paint().apply {
        color = Color.BLACK
        style = Paint.Style.FILL_AND_STROKE
        textSize = DisplayUtil.spToPx(14f)
        isAntiAlias = true
    }

    /**
     * 发光样式
     * [BlurMaskFilter.Blur.INNER]: 内发光
     * [BlurMaskFilter.Blur.SOLID]: 外发光
     * [BlurMaskFilter.Blur.NORMAL]: 内外发光
     * [BlurMaskFilter.Blur.OUTER]: 只显示发光部分
     */
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        var wStart = 110f
        paint.maskFilter = BlurMaskFilter(20f, BlurMaskFilter.Blur.INNER)
        canvas.drawCircle(wStart, 140f, 80f, paint)
        paint.maskFilter = BlurMaskFilter(20f, BlurMaskFilter.Blur.SOLID)
        wStart += 200f
        canvas.drawCircle(wStart, 140f, 80f, paint)
        paint.maskFilter = BlurMaskFilter(20f, BlurMaskFilter.Blur.NORMAL)
        wStart += 200f
        canvas.drawCircle(wStart, 140f, 80f, paint)
        paint.maskFilter = BlurMaskFilter(20f, BlurMaskFilter.Blur.OUTER)
        wStart += 200f
        canvas.drawCircle(wStart, 140f, 80f, paint)
        paint.maskFilter = EmbossMaskFilter(floatArrayOf(10f, 10f, 10f), 0.5f, 20f, 20f)
        wStart += 200f
        canvas.drawCircle(wStart, 140f, 80f, paint)

        canvas.drawBitmap(mBitmap, 50f, 230f, paint)
        canvas.drawBitmap(mAlphaBitmap, 50f, 230f, paint)
        paint.maskFilter = null
        canvas.drawBitmap(mBitmap, 250f, 230f, paint)
        canvas.drawBitmap(mAlphaBitmap, 250f, 230f, paint)
        paint.maskFilter = BlurMaskFilter(20f, BlurMaskFilter.Blur.NORMAL)
        paint.color = Color.BLUE
        canvas.drawBitmap(mAlphaBitmap, 450f, 230f, paint)
        canvas.drawBitmap(mBitmap, 450f, 230f, paint)


        val rectf = RectF(700f, 280f, 950f, 400f)
        paint.color = Color.parseColor("#1A6888B0")
        paint.maskFilter = BlurMaskFilter(20f, BlurMaskFilter.Blur.OUTER)
        canvas.drawRoundRect(rectf, 20f, 20f, paint)
        paint.color = Color.WHITE
        paint.maskFilter = null
        canvas.drawRoundRect(rectf, 20f, 20f, paint)


    }
}